package com.zh.note.huawei.twohundred.数状结构查询;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/8/24 10:09
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String s = sc.nextLine();
            String[] s1 = s.split(" ");
            map.computeIfAbsent(s1[1], k -> new ArrayList<>()).add(s1[0]);
        }
        String target = sc.next();
        List<String> res = new ArrayList<>();
        dfs(map, target, res);
        Collections.sort(res);
        res.stream().forEach(System.out::println);
    }

    private static void dfs(Map<String, List<String>> map, String target, List<String> res) {
        //出口
        if (!map.containsKey(target)) {
            return;
        }
        List<String> lists = map.get(target);
        for (int i = 0; i < lists.size(); i++) {
            String value = lists.get(i);
            res.add(value);
            if (map.containsKey(value)) {
                dfs(map, value, res);
            }
        }

    }
}
